package com.mzy.linear_struct.hot100.堆;

import com.sun.org.apache.xpath.internal.operations.String;

import java.util.*;

/**
 * @Author: mizy3
 * @Date: 2024/08/12/下午3:40
 * @Description:
 */
public class Pro347前K个高频元素 {

    public int[] topKFrequent(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }


        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((a, b) -> map.get(b) - map.get(a));
        priorityQueue.addAll(map.keySet());
        int[] ans = new int[k];
        for (int i = 0; i < k && !priorityQueue.isEmpty(); i++) {
            ans[i] = priorityQueue.poll();
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Pro347前K个高频元素().topKFrequent(new int[]{-1, -1}, 1)));
    }



















}
